//
//  MyActivitiesView.swift
//  letusmeet_ios
//
//  Created by Rovo Dev on 2025/1/27.
//

import SwiftUI

struct MyActivitiesView: View {
    let activities: [Activity]
    @State private var selectedActivity: Activity? = nil
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 标题和查看全部
            HStack {
                Text("我的活动")
                    .font(.system(size: 18, weight: .semibold))
                    .foregroundColor(.primary)
                
                Spacer()
                
                Button("查看全部") {
                    print("查看全部活动")
                }
                .font(.system(size: 14, weight: .medium))
                .foregroundColor(.orange)
            }
            .padding(.horizontal, 16)
            
            // 活动网格
            LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 12), count: 2), spacing: 12) {
                ForEach(activities, id: \.id) { activity in
                    ActivityCard(activity: activity) {
                        selectedActivity = activity
                    }
                }
            }
            .padding(.horizontal, 16)
        }
        .alert("活动详情", isPresented: .constant(selectedActivity != nil)) {
            Button("确定") {
                selectedActivity = nil
            }
        } message: {
            if let activity = selectedActivity {
                Text("活动：\(activity.title)\n地点：\(activity.location)")
            }
        }
    }
}

struct ActivityCard: View {
    let activity: Activity
    let onTap: () -> Void
    @State private var isPressed = false
    
    var body: some View {
        Button(action: onTap) {
            VStack(alignment: .leading, spacing: 8) {
                // 活动封面
                ZStack {
                    RoundedRectangle(cornerRadius: 8)
                        .fill(
                            LinearGradient(
                                colors: gradientColors(for: activity.coverImage),
                                startPoint: .topLeading,
                                endPoint: .bottomTrailing
                            )
                        )
                        .frame(height: 80)
                    
                    Text(iconForActivity(activity.coverImage))
                        .font(.system(size: 24))
                }
                
                // 活动信息
                VStack(alignment: .leading, spacing: 4) {
                    Text(activity.title)
                        .font(.system(size: 14, weight: .medium))
                        .foregroundColor(.primary)
                        .lineLimit(2)
                    
                    HStack(spacing: 4) {
                        Text("📍")
                            .font(.system(size: 10))
                        Text(activity.location)
                            .font(.system(size: 12))
                            .foregroundColor(.secondary)
                            .lineLimit(1)
                    }
                }
                .padding(.horizontal, 4)
            }
        }
        .buttonStyle(PlainButtonStyle())
        .scaleEffect(isPressed ? 0.95 : 1.0)
        .onLongPressGesture(minimumDuration: 0) { pressing in
            withAnimation(.easeInOut(duration: 0.1)) {
                isPressed = pressing
            }
        } perform: {
            onTap()
        }
        .background(Color(NSColor.controlBackgroundColor))
        .clipShape(RoundedRectangle(cornerRadius: 12))
        .shadow(color: .black.opacity(0.05), radius: 6, x: 0, y: 2)
    }
    
    private func gradientColors(for imageType: String) -> [Color] {
        switch imageType {
        case "hiking":
            return [.green.opacity(0.7), .blue.opacity(0.7)]
        case "reading":
            return [.purple.opacity(0.7), .pink.opacity(0.7)]
        case "photography":
            return [.orange.opacity(0.7), .red.opacity(0.7)]
        case "food":
            return [.yellow.opacity(0.7), .orange.opacity(0.7)]
        default:
            return [.gray.opacity(0.7), .blue.opacity(0.7)]
        }
    }
    
    private func iconForActivity(_ imageType: String) -> String {
        switch imageType {
        case "hiking":
            return "🥾"
        case "reading":
            return "📚"
        case "photography":
            return "📸"
        case "food":
            return "🍽️"
        default:
            return "🎯"
        }
    }
}

#Preview {
    let sampleActivities = [
        Activity(title: "周末户外徒步", location: "城市公园", coverImage: "hiking"),
        Activity(title: "经典小说分享", location: "咖啡书店", coverImage: "reading"),
        Activity(title: "周末摄影交流", location: "城市广场", coverImage: "photography"),
        Activity(title: "城市美食探店", location: "美食街", coverImage: "food")
    ]
    
    MyActivitiesView(activities: sampleActivities)
        .padding()
        .background(Color(NSColor.controlBackgroundColor))
}